package com.education.cms.service;

import com.education.cms.dao.UserDao;
import com.education.cms.domain.User;
import com.education.cms.dto.user.RegisterDto;
import com.education.cms.dto.user.ShowUserListDto;
import com.education.cms.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 用户登录
     */
    public User login(String username,String password){
        User user=userDao.loginUser(username,password);
        return user;
    }

    /**
     * 新用户录入（管理员权限
     */
    public String register(RegisterDto registerDto){
        if (userDao.findByUsername(registerDto.getUsername()) != null){
            return "该用户名已被注册！请更换";
        }else {
            User user=new User();
            user.setUsername(registerDto.getUsername());
            user.setPassword(registerDto.getPassword());
            user.setName(registerDto.getName());
            user.setBirthday(registerDto.getBirthday());
            user.setGender(registerDto.getGender());
            user.setPhone(registerDto.getPhone());
            user.setRole(registerDto.getRole());
            user.setStatus(registerDto.getStatus());
            userDao.save(user);
            return "注册成功！";
        }
    }
    /**
     * 查询用户(使用specification实现JPA的多条件复杂查询
     */
    public Page<User> showUserList(ShowUserListDto listDto, Pageable pageable){
        return userDao.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                //条件一：姓名
                if (!listDto.getName().isEmpty()){
                    list.add(criteriaBuilder.equal(root.<String>get("name"),listDto.getName()));
                }
                //条件二：年龄(模糊查询
                if (listDto.getAge() != null){
                    Integer birthYear = DateUtils.birthYear(listDto.getAge());
                    list.add(criteriaBuilder.like(root.get("birthday").as(String.class),"%"+birthYear+"%"));
                }
                //条件三：性别
                if (!listDto.getGender().isEmpty()){
                    list.add(criteriaBuilder.equal(root.<String>get("gender"),listDto.getGender()));
                }
                //条件四：角色
                if (listDto.getRole() != null){
                    list.add(criteriaBuilder.equal(root.<Integer>get("role"),listDto.getRole()));
                }
                //条件五：状态
                if (listDto.getStatus() != null){
                    list.add(criteriaBuilder.equal(root.<Integer>get("status"),listDto.getStatus()));
                }
                criteriaQuery.where(criteriaBuilder.and(list.toArray(new Predicate[list.size()])));
                return criteriaQuery.getRestriction();
            }
        },pageable);
    }

}
